package com.shop.controller;

import com.shop.common.SetMeta;
import com.shop.entity.Role;
import com.shop.exception.RoleException;
import com.shop.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表
     * @return
     */
    @PreAuthorize("hasAuthority('138')")
    @GetMapping("/roles")
    public SetMeta selectRoles() {
        return roleService.selectRoles();
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('129')")
    @PostMapping("/roles")
    public SetMeta insertRole(@RequestBody Role role) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = roleService.insertRole(role);
        } catch (RoleException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 根据id查询角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('139')")
    @GetMapping("/roles/{id}")
    public SetMeta selectRole(@PathVariable Integer id) {
        return roleService.selectRole(id);
    }

    /**
     * 编辑提交角色
     * @param id
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('140')")
    @PutMapping("/roles/{id}")
    public SetMeta updateRole(@PathVariable Integer id, @RequestBody Role role) {
        SetMeta setMeta = new SetMeta();
        try {
            role.setRoleId(id);
            setMeta = roleService.updateRole(role);
        } catch (RoleException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('130')")
    @DeleteMapping("/roles/{id}")
    public SetMeta deleteRole(@PathVariable Integer id) {
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = roleService.deleteRole(id);
        } catch (RoleException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 删除角色指定权限
     * @param roleId
     * @param rightId
     * @return
     */
    @PreAuthorize("hasAuthority('135')")
    @DeleteMapping("/roles/{roleId}/rights/{rightId}")
    public SetMeta deleteRoleRights(@PathVariable String roleId,@PathVariable String rightId){
        SetMeta setMeta = new SetMeta();
        try {
            setMeta = roleService.deleteRoleRights(Integer.parseInt(roleId),Integer.parseInt(rightId));
        } catch (RoleException e) {
            setMeta.setError(e.getMessage());
        }
        return setMeta;
    }

    /**
     * 角色授权
     * @param roleId
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('134')")
    @PostMapping("/roles/{roleId}/rights")
    public SetMeta rightsToRole(@PathVariable Integer roleId,@RequestBody Role role) {
        return roleService.rightsToRole(roleId,role);
    }

    //141 更新角色权限


}
